/*
Copyright (c) 2008,2009, David Beck

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**
   @file t__t1_server.cc
   @brief verifies the server side of various security models

   The actual security model used depends on the build. The following
   implementations are used:

   @li t__sec_null.cc the rxnull_* model
   @li t__sec_simple.cc hand crafted security model to see how Rx works
   @li t__sec_rt0d4.cc the rt0d4/Rx security model

   @file t__t1.ss.c
   @brief Automatically generated by rxgen
*/

#define RT0D4_WINDOWS_H_INCLUDED
#include "common.h"
#include "t__sec_common.h"
#include <assert.h>

#define SKIP_OPS_ONLY_TYPES
extern "C" {
  #include "t__t1.h"
}

RT0D4_CDECL
afs_int32 t__t1_ExecuteRequest(register struct rx_call *z_call);

RT0D4_CDECL
int t__t1_Add(
    /*IN */ struct rx_call *z_call,
    /*IN */ int a,
    /*IN */ int b,
    /*OUT*/ int * r) {
  *r = a + b;
  return 0;
}

RT0D4_CDECL
int t__t1_Dummy(
    /*IN */ struct rx_call *z_conn,
    /*IN */ struct opdata_t in,
    /*OUT*/ struct opdata_t * out)
{
  return 0;
}

typedef char * buffer_t;

RT0D4_CDECL
int t__t1_SendString(
    /*IN */ struct rx_call *z_call,
    /*IN */ char fill,
    /*IN */ buffer_t s)
{
  int maxerr = 10;
  int ret = 0;
  for( size_t i=0;i<T__T1_BUFFER_SIZE-1;++i )
    if( s[i] != fill ) {
      printf( "Error at: %d read %.2x indstead of %.2x\n", i,(unsigned char)s[i],fill );
      ret = -1;
      if( --maxerr < 0 )
      {
        printf("Too many errors. Stop reporting them.\n");
        fflush(stdout);
        break;
      }
    }
  return ret;
}

RT0D4_CDECL
int t__t1_RecvString(
    /*IN */ struct rx_call *z_call,
    /*IN */ char fill,
    /*OUT*/ buffer_t * s)
{
  *s = (char *)malloc(T__T1_BUFFER_SIZE);
  memset(*s,fill,T__T1_BUFFER_SIZE);
  (*s)[T__T1_BUFFER_SIZE-1]=0;
  return 0;
}

RT0D4_CDECL
int t__t1_SendStream(
    /*IN */ struct rx_call *z_call,
    /*IN */ char fill)
{
  char buff[80000];
  char * p = buff;
  int err = 0;
  unsigned long tmp;
  long sz;
  int maxerr = 20;
  int ret = 0;

  if( (err = rx_Read(z_call, (char *)&tmp, sizeof(tmp))) != sizeof(tmp) )
    return -1;

  sz = htonl( tmp );

  while( sz > 0 )
  {
    if( (err = rx_Read(z_call, p, sz )) <= 0 ) return -2;
    sz -= err;
    p  += err;
  }
  sz = htonl( tmp );
  p = buff;

  for( size_t i=0;i<(size_t)sz;++i )
    if( p[i] != fill ) {
      printf( "Error at: %d read %.2x indstead of %.2x\n", i,(unsigned char)p[i],fill );
      ret = -1;
      if( --maxerr < 0 )
      {
        printf("Too many errors. Stop reporting them.\n");
        fflush(stdout);
        break;
      }
    }
  return ret;
}

RT0D4_CDECL
int t__t1_RecvStream(
    /*IN */ struct rx_call *z_call,
    /*IN */ char fill)
{
  char buff[80000];
  unsigned long sz = htonl(sizeof(buff));
  memset( buff,fill,sizeof(buff) );
  int err;
  if( (err=rx_Write(z_call, (char *)&sz, sizeof(sz) )) != sizeof(sz) ) return -1;
  if( (err=rx_Write(z_call, buff, sizeof(buff) )) != sizeof(buff) ) return -2;
  return 0;
}

/** @brief Checks how different Rx security models work */
namespace test_t1_rx {

int t1_test_server(int argc, char ** argv)
{
  const char * errstr = "OK";
  struct rx_securityClass *(sec_p[1]);
  struct rx_service * service_p = 0;
  unsigned short svr_port = T__T1_SERVER_PORT;

  if (rx_Init(htons(svr_port))) {
    errstr ="rx_Init";
    goto error;
  }
  if (!(sec_p[0] = get_server_security())) {
    errstr = "NewServerSecurityObject";
    goto error;
  }
  if (!(service_p = rx_NewService(
                     0,                   /* port */
                     T__T1_SERVICE_ID,
                     "t__t1",             /* service name */
                     sec_p,               /* secobjs */
                     1,                   /* n secobjs */
                     t__t1_ExecuteRequest))) {
    errstr = "NewService";
    goto error;
  }
  rx_StartServer(1);

  rx_Finalize();
  printf("OK: %s\n",errstr);
  return 0;

error:
  printf("ERROR: %s\n",errstr);
  return 0;
}

} // end of test_t1_rx

using namespace test_t1_rx;

int main( int argc, char ** argv )
{
  return t1_test_server(argc, argv);
}

/* EOF */
